/*
 *
 *  Copyright (C) 2010 Tal Eisenberg
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package tal.drivechain.system;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.URL;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;

/**
 *
 * @author eisental
 */
public class Splasher implements Runnable {
    private final static int WIDTH = 300, HEIGHT = 200;

    OutputStream out = null;
    JTextArea text;
    private JFrame frame;

    public Splasher() {
        out = new PrintStream(new SplasherStream());
        frame = new JFrame();
        frame.setAlwaysOnTop(true);
        text = new JTextArea(10,80);
        text.setFont(new Font("courier", Font.PLAIN, 12));
        text.setForeground(Color.WHITE);
        text.setLineWrap(true);
        text.setOpaque(false);
        text.setEditable(false);
        JScrollPane scroll = new JScrollPane(text);
        scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
        scroll.setOpaque(false);
        scroll.getViewport().setOpaque(false);
        scroll.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
        ImagePanel panel = new ImagePanel(this.getClass().getResource("/resources/chain.png"));
        panel.setBackground(Color.BLACK);
        panel.setBorder(BorderFactory.createLineBorder(Color.black));
        
        JLabel versionLabel = new JLabel(Globals.VERSION.toString());
        versionLabel.setFont(new Font("sans serif", Font.BOLD, 14));
        versionLabel.setHorizontalAlignment(SwingConstants.LEFT);
        versionLabel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
        versionLabel.setForeground(Color.WHITE);

        JLabel authorLabel = new JLabel("Tal Eisenberg (oct 2010)");
        authorLabel.setFont(new Font("serif", Font.BOLD, 12));
        authorLabel.setHorizontalAlignment(SwingConstants.RIGHT);
        authorLabel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
        authorLabel.setForeground(Color.WHITE);
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1;
        c.weighty = 0;
        c.gridy = 0;
        panel.add(versionLabel, c);

        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1;
        c.weighty = 1;
        c.gridy = 1;
        panel.add(scroll, c);

        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1;
        c.weighty = 0;
        c.gridy = 2;
        panel.add(authorLabel, c);

        frame.setUndecorated(true);
        frame.add(panel);
        frame.pack();
        Point p = new Point((Toolkit.getDefaultToolkit().getScreenSize().width - frame.getWidth())/2,
                (Toolkit.getDefaultToolkit().getScreenSize().height - frame.getHeight())/2);
        frame.setLocation(p);

    }

    public void destroy() {
        if (frame!=null) {
            frame.removeAll();
            frame.setVisible(false);
            frame = null;
        } if (text!=null) text = null;
    }

    public void run() {
        frame.setVisible(true);
    }

    public OutputStream getOutput() { return out; }

    public void showAbout(String aboutText) {
        MouseAdapter m = new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                destroy();
            }
        };
        frame.addMouseListener(m);
        for (Component c : frame.getComponents())
            c.addMouseListener(m);
        text.addMouseListener(m);
        frame.addWindowFocusListener(new WindowFocusListener(){
            public void windowGainedFocus(WindowEvent e) {}

            public void windowLostFocus(WindowEvent e) {
                destroy();
            }
        });
        text.setText(aboutText);
        frame.setVisible(true);
    }

    public void showAbout() {
        showAbout("");
    }

    class SplasherStream extends OutputStream {
        String line = "";
        boolean foundNewLine = false;
        @Override
        public void write(int b) throws IOException {
            line += (char)b;

            if (b=='\n') {
                throwOut();
            }
        }

        private void throwOut() {
            if (text!=null) {
                text.append(line.trim()+"\n");
                int lastline = text.getText().lastIndexOf("\n")+1;
                if (lastline>=text.getText().length()) lastline = text.getText().length()-1;
                text.setCaretPosition(text.getText().lastIndexOf("\n")+1);
            }
            line = "";
        }

    }

class ImagePanel extends JPanel {

  private Image img;

  public ImagePanel(URL url) {
    this(new ImageIcon(url).getImage());
  }

  public ImagePanel(Image img) {
    this.img = img;
    Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
    setPreferredSize(size);
    setMinimumSize(size);
    setMaximumSize(size);
    setSize(size);
    setLayout(new GridBagLayout());
  }

  @Override
  public void paintComponent(Graphics g) {
    g.drawImage(img, 0, 0, null);
  }

}

}
